import 'dart:math';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import '../../../resources/kq_theme_colors.dart';
import '../../../resources/l10n/kq_intl.dart';
import '../kq_chart_line_model.dart';
import 'package:path_drawing/path_drawing.dart';
import 'Kq_line_monotone_x.dart';

class KqChartMethod {
  //计算折线曲线运动路径
  static List getAnimationLinePath(Path path, List<ChartLineValue> points,
      List xList, List yList, double animationTime, bool isAnimationEnd) {
    PathMetrics ma = path.computeMetrics();
    Path newPath = Path();
    Path shadowPath = Path();
    int pointIndex = 0;
    Tangent? lastPoint;
    while (ma.iterator.moveNext()) {
      PathMetric mm = ma.iterator.current;
      newPath.addPath(
          mm.extractPath(0, animationTime * mm.length), Offset.zero);
      shadowPath.addPath(
          mm.extractPath(0, animationTime * mm.length), Offset.zero);
      lastPoint = mm.getTangentForOffset(animationTime * mm.length);
      pointIndex = (animationTime / (xList[1] - xList.first)).ceil();
      break;
    }
    pointIndex = isAnimationEnd ? xList.length : pointIndex;
    // print(lastPoint?.position.dx);
    shadowPath.lineTo(lastPoint?.position.dx ?? xList.last, yList.last);
    shadowPath.lineTo(xList.first, yList.last);
    shadowPath.close();
    return [newPath, shadowPath, pointIndex];
  }

  //计算柱状运动路径
  static Path getAnimationBarPath(
      Path path, double animationTime, bool isAnimationEnd) {
    PathMetrics ma = path.computeMetrics();
    Path newPath = Path();
    while (ma.iterator.moveNext()) {
      PathMetric mm = ma.iterator.current;
      newPath.addPath(
          mm.extractPath(0, animationTime * mm.length), Offset.zero);
      break;
    }
    if (isAnimationEnd) {
      newPath = path;
    }
    return newPath;
  }

  ///计算曲线坐标
  static Path getSmoothLinePath(List<ChartLineValue> values) {
    List<Point> points = [];
    values.map((ChartLineValue e) {
      points.add(Point(e.xPoint, e.yPoint));
    }).toList();
    var targetPoints = <Point>[];
    targetPoints.addAll(points);
    targetPoints.add(Point(
        points[points.length - 1].x * 2, points[points.length - 1].y * 2));
    double? x0, y0, x1, y1, t0;
    var path = Path();
    for (int i = 0; i < targetPoints.length; i++) {
      double? t1;
      var x = targetPoints[i].x;
      var y = targetPoints[i].y;
      if (x == x1 && y == y1) break;
      switch (i) {
        case 0:
          path.moveTo(x as double, y as double);
          break;
        case 1:
          break;
        case 2:
          t1 = MonotoneX.slope3(x0!, y0!, x1!, y1!, x as double, y as double);
          MonotoneX.point(
              path, x0, y0, x1, y1, MonotoneX.slope2(x0, y0, x1, y1, t1), t1);
          break;
        default:
          t1 = MonotoneX.slope3(x0!, y0!, x1!, y1!, x as double, y as double);
          MonotoneX.point(path, x0, y0, x1, y1, t0!, t1);
      }
      x0 = x1;
      y0 = y1;
      x1 = x as double;
      y1 = y as double;
      t0 = t1;
    }
    return path;
  }

  //绘制X轴Y轴，Y轴分割线,Y轴上文案内容，X轴上文案内容
  static void drawXYAxisPath({
    required Canvas canvas,
    required Paint paint,
    required Color axisColor,
    required List xList,
    required List yList,
    required double xRight,
    required bool isDivide,
    required Color dividerColor,
    required bool hintLineSolid,
    required List crossShowList,
    required TextStyle yStyle,
    required TextStyle xStyle,
    required List yValue,
    required List xValue,
    required double yAisTop,
    String? unit,
    double initialPointX = 0,
  }) {
    paint.color = axisColor;
    Path horizontalPath = Path();
    horizontalPath.moveTo(xList.first - initialPointX, yList.last);
    horizontalPath.lineTo(xList.last + xRight, yList.last);
    canvas.drawPath(horizontalPath, paint);

    Path verticalPath = Path();
    verticalPath.moveTo(xList.first - initialPointX, yAisTop);
    verticalPath.lineTo(xList.first - initialPointX, yList.last);
    canvas.drawPath(verticalPath, paint);
    KqChartMethod.drawYValue(
        canvas: canvas,
        yValue: yValue,
        xList: xList,
        yList: yList,
        yStyle: yStyle,
        initialPointX: initialPointX);
    KqChartMethod.drawXValue(
        canvas: canvas,
        crossShowList: crossShowList,
        xValue: xValue,
        xList: xList,
        yList: yList,
        xStyle: xStyle);

    if (unit != null) {
      var textY = TextPainter(
          textAlign: TextAlign.right,
          ellipsis: '.',
          maxLines: 1,
          text: TextSpan(
              text: unit,
              style: TextStyle(fontSize: 10.sp, color: KqThemeColors.textBF)),
          textDirection: TextDirection.ltr)
        ..layout();
      textY.paint(canvas, Offset(xList.first - initialPointX - 29.r, 0));
    }

    if (isDivide == true) {
      paint.color = dividerColor;
      for (int i = 0; i < yList.length - 1; i++) {
        Path dividerPath = Path();
        dividerPath.moveTo(xList.first - initialPointX, yList[i]);
        dividerPath.lineTo(xList.last + xRight, yList[i]);
        if (hintLineSolid) {
          canvas.drawPath(dividerPath, paint);
        } else {
          canvas.drawPath(
            dashPath(
              dividerPath,
              dashArray: CircularIntervalList<double>(<double>[4.0, 2.0]),
            ),
            paint,
          );
        }
      }
    }
  }

  ///绘制平均线
  static void drawAvgLine({
    required Canvas canvas,
    required Paint paint,
    required List avgYPointList,
    required bool isAvg,
    required List xList,
    required double xRight,
    List<Color>? avgColors,
    required List avgValues,
    required bool isAvgValue,
    required bool hintAvgLineSolid,
    double initialPointX = 0,
  }) {
    if (isAvg == true) {
      for (int i = 0; i < avgYPointList.length; i++) {
        if (avgColors != null && avgColors.length > i) {
          paint.color = avgColors[i];
        } else {
          paint.color = KqThemeColors.textLightBlue;
        }
        Path dividerPath = Path();
        dividerPath.moveTo(xList.first - initialPointX, avgYPointList[i]);
        dividerPath.lineTo(xList.last + xRight, avgYPointList[i]);
        if (hintAvgLineSolid) {
          canvas.drawPath(dividerPath, paint);
        } else {
          canvas.drawPath(
            dashPath(
              dividerPath,
              dashArray: CircularIntervalList<double>(<double>[4.0, 2.0]),
            ),
            paint,
          );
        }
      }
      if (isAvgValue == true) {
        double xRightPoint = xList.last + xRight;
        for (var i = 0; i < avgValues.length; i++) {
          String value = avgValues[i];
          var avgText = TextPainter(
              textAlign: TextAlign.right,
              ellipsis: '.',
              maxLines: 1,
              text: TextSpan(
                  text: '${KqIntl.currentResource.averageValue}:$value',
                  style:
                      TextStyle(fontSize: 10.sp, color: KqThemeColors.text59)),
              textDirection: TextDirection.ltr)
            ..layout();
          avgText.paint(canvas, Offset(xRightPoint - avgText.size.width, 0));
          xRightPoint = xRightPoint - avgText.size.width - 4.r;

          if (avgColors != null && avgColors.length > i) {
            paint.color = avgColors[i];
          } else {
            paint.color = KqThemeColors.textLightBlue;
          }
          Path dividerPath = Path();
          dividerPath.moveTo(xRightPoint, 8.sp);
          dividerPath.lineTo(xRightPoint - 10.r, 8.sp);
          if (hintAvgLineSolid) {
            canvas.drawPath(dividerPath, paint);
          } else {
            canvas.drawPath(
              dashPath(
                dividerPath,
                dashArray: CircularIntervalList<double>(<double>[4.0, 2.0]),
              ),
              paint,
            );
          }
          xRightPoint = xRightPoint - 10.r - 20.r;
        }
      }
    }
  }

  static void drawUnitValue() {}

  /// 绘制y轴文本
  static void drawYValue({
    required Canvas canvas,
    required List yValue,
    required List xList,
    required List yList,
    required TextStyle yStyle,
    double initialPointX = 0,
  }) {
    for (int i = 0; i < yValue.length; i++) {
      var textY = TextPainter(
          textAlign: TextAlign.right,
          ellipsis: '.',
          maxLines: 1,
          text: TextSpan(text: yValue[i], style: yStyle),
          textDirection: TextDirection.ltr)
        ..layout();
      textY.paint(
          canvas,
          Offset(xList.first - initialPointX - textY.width - 6,
              yList[i] - textY.height / 2));
    }
  }

  /// 绘制x轴文本
  static void drawXValue({
    required Canvas canvas,
    required List crossShowList,
    required List xValue,
    required List xList,
    required List yList,
    required TextStyle xStyle,
  }) {
    if (xValue.length > 1) {
      if (crossShowList.isNotEmpty) {
        for (int i = 0; i < crossShowList.length; i++) {
          int index = crossShowList[i];
          var textX = TextPainter(
              textAlign: TextAlign.center,
              ellipsis: '.',
              maxLines: 1,
              text: TextSpan(text: xValue[index], style: xStyle),
              textDirection: TextDirection.ltr)
            ..layout();
          textX.paint(
              canvas, Offset(xList[index] - textX.width / 2, yList.last + 6));
        }
      } else {
        for (int i = 0; i < xList.length; i++) {
          if (xValue.length > i) {
            var textX = TextPainter(
                textAlign: TextAlign.center,
                ellipsis: '.',
                maxLines: 1,
                text: TextSpan(text: xValue[i], style: xStyle),
                textDirection: TextDirection.ltr)
              ..layout();
            textX.paint(
                canvas, Offset(xList[i] - textX.width / 2, yList.last + 6));
          }
        }
      }
    } else {
      if (xValue.isNotEmpty) {
        var textX = TextPainter(
            textAlign: TextAlign.center,
            ellipsis: '.',
            maxLines: 1,
            text: TextSpan(text: xValue.first, style: xStyle),
            textDirection: TextDirection.ltr)
          ..layout();
        textX.paint(
            canvas,
            Offset(
                xList.first + (xList.last - xList.first) / 2 - textX.width / 2,
                yList.last + 6));
      }
    }
  }
}


          // canvas.save();
          // canvas.clipPath(path)
          // canvas.drawImage(image, offset, paint)
          // canvas.restore()